/**
 * 
 */
package com.jonosoft.photocast.viewer.web.client.ui;

import java.util.ArrayList;
import java.util.Iterator;

import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.ui.ComplexPanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.LoadListener;
import com.google.gwt.user.client.ui.Widget;
import com.jonosoft.photocast.viewer.web.client.LibraryImageMap;
import com.jonosoft.photocast.viewer.web.client.debug.Logger;

/**
 * TODO Add description ImageGrid (com.jonosoft.photocast.viewer.web.client.ImageGrid)
 *
 * @author jon
 *
 */
public class ThumbnailsPanel extends Composite {
	
	private LibraryImageMap libraryImageMap = null;
	private int imageWidth = 0;
	private int imageHeight = 0;
	private int imageSpacing = 2;
	private int backgroundColorGrayValue = 0;
	private final FlowPanel panel = new FlowPanel();
	private final DeferredImagePopulator deferredImagePopulator = new DeferredImagePopulator(panel);

	public ThumbnailsPanel(ArrayList imageURLs, int imageWidth, int imageHeight) {
		initWidget(panel);

    	panel.setStyleName("thumbnails-panel");
    	panel.setWidth("100%");
		
		setImages(libraryImageMap);
	}

	public LibraryImageMap getImages() {
    	return this.libraryImageMap;
    }

	public int getImageWidth() {
    	return this.imageWidth;
    }

	public void setImages(LibraryImageMap imageMap) {
    	this.libraryImageMap = imageMap;
    }

	public void setImageWidth(int imageWidth) {
    	this.imageWidth = imageWidth;
    }
	
	public void setImageHeight(int imageHeight) {
		this.imageHeight = imageHeight;
	}
	
    public void populateImageGrid() {
    	if (libraryImageMap != null) {
    		// set all display types for thumbnail
    		Iterator i = libraryImageMap.values().iterator();
    		
    		while (i.hasNext())
    			((LibraryImage) i.next()).setDisplayType(1);
    		
    		deferredImagePopulator.populateImages(libraryImageMap);
    	}
    }
    
    private class DeferredImagePopulator implements LoadListener, Command {
    	
    	private LibraryImageMap imageMap = null;
    	private Iterator imageMapIterator = null;
    	private ComplexPanel imagesPanel = null;
    	private boolean isRunning = false;

		public DeferredImagePopulator(ComplexPanel panel) {
        	this.imagesPanel = panel;
        }
		
		public void populateImages(LibraryImageMap imageMap) {
			this.imageMap = imageMap;
			this.imageMapIterator = imageMap.keySet().iterator();
			
			if (!isRunning)
				nextImage();
		}

		/**
         * @see com.google.gwt.user.client.Command#execute()
         */
        public void execute() {
        	nextImage();
        }
		
		/**
		 * @see com.google.gwt.user.client.ui.LoadListener#onError(com.google.gwt.user.client.ui.Widget)
		 */
		public void onError(Widget sender) {
			try {
				Image img = (Image) sender;
				Logger.debug("Error loading image: " + img.getUrl());
				nextImage();
			}
			catch (Exception ignored) {
				return;
			}
		}
		
		/**
		 * @see com.google.gwt.user.client.ui.LoadListener#onLoad(com.google.gwt.user.client.ui.Widget)
		 */
		public void onLoad(Widget sender) {
			try {
				Image img = (Image) sender;
				Logger.debug("Loaded image: " + img.getUrl());
				nextImage();
			}
			catch (Exception ignored) {
				return;
			}
		}
        
        private void nextImage() {
        	int count = 0;
        	
        	while (imageMapIterator.hasNext()) {
        		String guid = (String) imageMapIterator.next();
        		LibraryImage libraryImage = (LibraryImage) imageMap.get(guid);
        		
        		imagesPanel.add(libraryImage);
        		
        		if (imageMapIterator.hasNext()) {
        			isRunning = true;
        			if (++count % 3 == 0) {
        				DeferredCommand.add(this);
        				return;
        			}
        		}
        	}
        	
        	isRunning = false;
        }
    	
    }

}
